<?php
/**
 * Descript:
 * User: lufeng501206@gmail.com
 * Date: 2017/4/9 18:59
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\MessagesTemplate;
use App\Repositories\TasksRepos;
use App\Repositories\UsersRepos;
use Illuminate\Http\Request;

class ManagerController extends BaseController
{
    private $pageLimit;
    private $tasksRepos;
    private $usersRepos;

    public function __construct(TasksRepos $tasksRepos, UsersRepos $usersRepos)
    {
        $this->middleware('verify.admin');
        $this->pageLimit = 10;
        $this->tasksRepos = $tasksRepos;
        $this->usersRepos = $usersRepos;
    }

    public function myPower()
    {
        $data['auth'] = $this->getUserAuth();
        return view('system.my-power',$data);
    }

    public function sendMsg()
    {
        $data['auth'] = $this->getUserAuth();
        return view('system.send-msg',$data);
    }

    /**
     * 消息模板
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function msgTrigger()
    {
        $data['auth'] = $this->getUserAuth();
        return view('system.msg-trigger',$data);
    }

    /**
     * 获取群发模板消息列表
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getTriggerLists()
    {
        $lists = MessagesTemplate::where('type',2)->get()->toArray();
        $data['lists'] = $lists;
        return successJson($data);
    }

    /**
     * 保存消息模板
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function saveTrigger(Request $request)
    {
        $inputs = $request->input();
        $id = $inputs['id'];
        $messages = $inputs['messages'];
        $msgModel = MessagesTemplate::findOrFail($id);
        $msgModel->messages = $messages;
        $res = $msgModel->save();
        if ($res) {
            return successJson();
        }else{
            return errorJson();
        }
    }

    /**
     * 获取用户列表数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function getUsersLists(Request $request)
    {
        $inputs = $request->input();
        $conditions['start'] = !empty($inputs['current']) ? ($inputs['current'] - 1) * $this->pageLimit : 0;
        $conditions['limit'] = $this->pageLimit;
        if (!empty($inputs['type']) && !empty($inputs['keyword'])) {
            $conditions[$inputs['type']] = $inputs['keyword'];
        }
        $data = $this->usersRepos->getUsersLists($conditions, false);
        $data['identity'] = config('jinbell.identity');
        return successJson($data);
    }

    /**
     * 群发消息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function sendMessages(Request $request)
    {
        $data = $inputs = $request->input();

        $messages = [
            'title.required' => '发送内容不能为空！',
            'messages.required' => '发送内容不能为空！',
        ];
        $validator = \Validator::make($data, [
            'title' => 'required',
            'messages' => 'required',
        ], $messages);

        if ($validator->fails()) {
            $errors = $validator->errors()->messages();
            $errorsMsg = "请检查表单参数：";
            foreach ($errors as $key => $value) {
                $errorsMsg .= $value[0];
            }
            return errorJson($errors, $errorsMsg);
        }
        $title = $inputs['title'];
        $messages = $inputs['messages'];
        $isVerifyAll = $inputs['checkboxall'];
        if ($isVerifyAll) {
            $conditions = [];
            if (!empty($inputs['type']) && !empty($inputs['keyword'])) {
                $conditions[$inputs['type']] = $inputs['keyword'];
            }
            $userlists = $this->usersRepos->getUsersLists($conditions);
            $userlists = array_keys(rebulidDataIndex($userlists));
        } else {
            $userlists = $inputs['userlists'];
        }
        \DB::beginTransaction();
        $templateResId = $this->usersRepos->createMessagesTemplate($title, $messages);
        if (empty($templateResId)) {
            \DB::rollBack();
            return errorJson([], '操作失败');
        }
        $readerRes = $this->usersRepos->createMessagesReader($templateResId, $userlists);
        if (empty($readerRes)) {
            \DB::rollBack();
            return errorJson([], '操作失败');
        }
        \DB::commit();
        return successJson([], '操作成功');
    }
}